package com.data.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.data.modules.online.cgform.entity.OnlCgformField;
import com.data.modules.online.cgform.entity.OnlCgformHead;
import com.data.modules.online.cgform.mapper.OnlCgformFieldMapper;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.util.OnlineImportValidator;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import com.data.common.util.SpringContextUtils;
import com.data.modules.online.cgform.converter.ConvertUtil;
import com.data.modules.online.cgform.service.IOnlCgformHeadService;
import com.data.modules.online.cgform.service.IOnlCgformSqlService;
import com.data.modules.online.config.exception.BusinessException;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OnlCgformSqlServiceImpl implements IOnlCgformSqlService {
    @Autowired
    @Lazy(true)
    private IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public void saveBatchOnlineTable(OnlCgformHead head, List<OnlCgformField> fieldList, List<Map<String, Object>> dataList) throws BusinessException {
        int i = 0;
        SqlSession sqlSession = null;
        try {
            ConvertUtil.m131a(2, dataList, fieldList);
            SqlSession openSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
            OnlCgformFieldMapper onlCgformFieldMapper = (OnlCgformFieldMapper) openSession.getMapper(OnlCgformFieldMapper.class);
            if (1000 >= dataList.size()) {
                while (i < dataList.size()) {
                    m180a(JSON.toJSONString(dataList.get(i)), head, fieldList, onlCgformFieldMapper);
                    i++;
                }
            } else {
                while (i < dataList.size()) {
                    m180a(JSON.toJSONString(dataList.get(i)), head, fieldList, onlCgformFieldMapper);
                    if (i % 1000 == 0) {
                        openSession.commit();
                        openSession.clearCache();
                    }
                    i++;
                }
            }
            openSession.commit();
            openSession.close();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            throw new BusinessException(e.getMessage());
        } catch (Throwable th) {
            sqlSession.close();
            throw th;
        }
    }

    @Override
    public void saveOrUpdateSubData(String subDataJsonStr, OnlCgformHead head, List<OnlCgformField> subFiledList) throws BusinessException {
        m180a(subDataJsonStr, head, subFiledList, (OnlCgformFieldMapper) SpringContextUtils.getBean(OnlCgformFieldMapper.class));
    }

    @Override
    public Map<String, String> saveOnlineImportDataWithValidate(OnlCgformHead head, List<OnlCgformField> fieldList, List<Map<String, Object>> dataList) {
        StringBuffer stringBuffer = new StringBuffer();
        OnlineImportValidator gVar = new OnlineImportValidator(fieldList);
        OnlCgformFieldMapper onlCgformFieldMapper = (OnlCgformFieldMapper) SpringContextUtils.getBean(OnlCgformFieldMapper.class);
        int size = dataList.size();
        int i = 0;
        int i2 = 0;
        int i3 = 0;
        while (i < size) {
            String jSONString = JSON.toJSONString(dataList.get(i));
            int i4 = i3 + 1;
            String a = gVar.checkFieldValidType(jSONString, i4);
            if (a == null) {
                try {
                    m180a(jSONString, head, fieldList, onlCgformFieldMapper);
                } catch (Exception e) {
                    i2++;
                    stringBuffer.append(OnlineImportValidator.formatCheck(m179a(e.getCause().getMessage()), i4));
                }
            } else {
                i2++;
                stringBuffer.append(a);
            }
            i++;
            i3 = i4;
        }
        HashMap hashMap = new HashMap();
        hashMap.put(OnlineImportValidator.f336a, stringBuffer.toString());
        hashMap.put(OnlineImportValidator.f337b, OnlineImportValidator.formatRemind(size, i2));
        return hashMap;
    }

    private void m180a(String str, OnlCgformHead onlCgformHead, List<OnlCgformField> list, OnlCgformFieldMapper onlCgformFieldMapper) throws BusinessException {
        JSONObject parseObject = JSONObject.parseObject(str);
        int executeEnhanceJava = this.onlCgformHeadService.executeEnhanceJava("import", CgformUtil.f279ao, onlCgformHead, parseObject);
        String tableName = onlCgformHead.getTableName();
        if (1 == executeEnhanceJava) {
            onlCgformFieldMapper.executeInsertSQL(CgformUtil.m241a(tableName, list, parseObject));
        } else if (2 == executeEnhanceJava) {
            onlCgformFieldMapper.executeUpdatetSQL(CgformUtil.m269b(tableName, list, parseObject));
        } else {
            if (executeEnhanceJava == 0) {
            }
        }
    }

    private String m179a(String str) {
        Matcher matcher = Pattern.compile("^Duplicate entry \\'(.*)\\' for key .*$").matcher(str);
        if (matcher.find()) {
            return "重复数据" + matcher.group(1);
        }
        return str;
    }
}
